package com.gly091020;

import net.minecraft.util.math.random.Random;

public class MathDragonFun{
    public enum Symbols{
        ADDITION,SUBTRACTION,MULTIPLICATION,DIVISION;
        public float getAnswer(float num1, float num2){
            switch (this){
                case ADDITION -> {return num1 + num2;}
                case SUBTRACTION -> {return num1 - num2;}
                case MULTIPLICATION -> {return num1 * num2;}
                case DIVISION -> {return num1 / num2;}
            }
            return 0;
        }

        public String getSymbolString(){
            switch (this){
                case ADDITION -> {return "+";}
                case SUBTRACTION -> {return "-";}
                case MULTIPLICATION -> {return "×";}
                case DIVISION -> {return "÷";}
            }
            return "?";
        }

        static public Symbols getRandomSymbol(){
            switch (Random.create().nextBetween(0, 3)){
                case 0 -> {return Symbols.ADDITION;}
                case 1 -> {return Symbols.SUBTRACTION;}
                case 2 -> {return Symbols.MULTIPLICATION;}
                case 3 -> {return Symbols.DIVISION;}
            }
            return Symbols.ADDITION;
        }
    }
    static public class Mathematical{
        public float Num1;
        public float Num2;
        public Symbols Symbol;
        public float Answer;
        Mathematical(float num1, float num2, Symbols symbol){
            Num1 = num1;
            Num2 = num2;
            Symbol = symbol;
            Answer = symbol.getAnswer(num1, num2);
        }
    }
    static public Mathematical getRandomMathematical(){
        Random random = Random.create();
        Symbols symbol = getSymbol();
        int num1 = random.nextBetween(0, 100);
        int num2 = random.nextBetween(0, 100);
        switch (symbol){
            case ADDITION -> {
                num1 = random.nextBetween(MathDragon.CONFIG.mathematicalSetting.addition.count.getMin(),
                        MathDragon.CONFIG.mathematicalSetting.addition.count.getMax());
                if (MathDragon.CONFIG.mathematicalSetting.addition.overTheLimit){
                    num2 = random.nextBetween(MathDragon.CONFIG.mathematicalSetting.addition.count.getMin(),
                            MathDragon.CONFIG.mathematicalSetting.addition.count.getMax());
                }else {
                    num2 = random.nextBetween(MathDragon.CONFIG.mathematicalSetting.addition.count.getMin(),
                            MathDragon.CONFIG.mathematicalSetting.addition.count.getMax() - num1);
                }
            }
            case SUBTRACTION -> {
                num1 = random.nextBetween(MathDragon.CONFIG.mathematicalSetting.subtraction.count.getMin(),
                        MathDragon.CONFIG.mathematicalSetting.subtraction.count.getMax());
                if (MathDragon.CONFIG.mathematicalSetting.subtraction.overTheLimit){
                    num2 = random.nextBetween(MathDragon.CONFIG.mathematicalSetting.subtraction.count.getMin(),
                            MathDragon.CONFIG.mathematicalSetting.subtraction.count.getMax());
                }else{
                    num2 = random.nextBetween(MathDragon.CONFIG.mathematicalSetting.subtraction.count.getMin(), num1);
                }
            }
            case MULTIPLICATION -> {
                int t = 0;
                do {
                    num1 = random.nextBetween(MathDragon.CONFIG.mathematicalSetting.multiplication.num1.getMin(),
                            MathDragon.CONFIG.mathematicalSetting.multiplication.num1.getMax());
                    num2 = random.nextBetween(MathDragon.CONFIG.mathematicalSetting.multiplication.num1.getMin(),
                            MathDragon.CONFIG.mathematicalSetting.multiplication.num2.getMax());
                    t++;
                    if (t >= 100){
                        break;
                    }
                } while (num1 + num2 > MathDragon.CONFIG.mathematicalSetting.multiplication.count.getMax() &&
                        !MathDragon.CONFIG.mathematicalSetting.multiplication.overTheLimit);
            }
            case DIVISION -> {
                if (MathDragon.CONFIG.mathematicalSetting.division.overTheLimit) {
                    num1 = random.nextBetween(MathDragon.CONFIG.mathematicalSetting.division.num1.getMin(),
                            MathDragon.CONFIG.mathematicalSetting.division.num1.getMax());
                    do {
                        num2 = random.nextBetween(MathDragon.CONFIG.mathematicalSetting.division.num2.getMin(),
                                MathDragon.CONFIG.mathematicalSetting.division.num2.getMax());
                    } while (num2 == 0);
                } else {
                    do {
                        num2 = random.nextBetween(MathDragon.CONFIG.mathematicalSetting.division.num2.getMin(),
                                MathDragon.CONFIG.mathematicalSetting.division.num2.getMax());
                    } while (num2 == 0);
                    int multiplier = random.nextBetween(
                            MathDragon.CONFIG.mathematicalSetting.division.num1.getMin() / num2,
                            MathDragon.CONFIG.mathematicalSetting.division.num1.getMax() / num2);
                    if (multiplier == 0) {
                        multiplier = 1;
                    }
                    num1 = multiplier * num2;
                }
            }
        }
        return new Mathematical(num1, num2, symbol);
    }

    private static Symbols getSymbol() {
        Symbols symbol;
        do {
            symbol = Symbols.getRandomSymbol();
        }while ((symbol == Symbols.ADDITION && !MathDragon.CONFIG.mathematicalSetting.addition.enable) ||
                (symbol == Symbols.SUBTRACTION && !MathDragon.CONFIG.mathematicalSetting.subtraction.enable) ||
                (symbol == Symbols.DIVISION && !MathDragon.CONFIG.mathematicalSetting.division.enable) ||
                (symbol == Symbols.MULTIPLICATION && !MathDragon.CONFIG.mathematicalSetting.multiplication.enable));
        return symbol;
    }
}
